import { PayloadMcuid, PayloadUser } from '@libs/common/decorator';
import {
  IndexInterceptor,
  TransactionInterceptor,
} from '@libs/common/interceptors';
import { Body, Controller, Post, UseInterceptors } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';

import {
  BuyProductDto,
  CreateProductDto,
  DeleteProductDto,
  IndexProductDto,
  ShowProductDto,
  SwitchProductDto,
  UpdateProductDto,
} from './dto/product.dto';
import { ProductService } from './product.service';

@ApiTags('商品')
@Controller('market/product')
export class ProductController {
  constructor(private readonly productService: ProductService) {}

  @Post('index')
  @UseInterceptors(new IndexInterceptor())
  index(@Body() dto: IndexProductDto) {
    return this.productService.index(dto);
  }

  @Post('show')
  show(@Body() dto: ShowProductDto) {
    return this.productService.show(dto);
  }

  @Post('delete')
  delete(@Body() dto: DeleteProductDto) {
    return this.productService.delete(dto);
  }

  @Post('create')
  create(@Body() dto: CreateProductDto) {
    return this.productService.create(dto);
  }

  @Post('update')
  update(@Body() dto: UpdateProductDto) {
    return this.productService.update(dto);
  }

  @Post('switch')
  switch(@Body() dto: SwitchProductDto) {
    return this.productService.switch(dto.id, dto.status);
  }

  @Post('buy')
  @UseInterceptors(TransactionInterceptor)
  buy(
    @Body() dto: BuyProductDto,
    @PayloadUser() user: SC.User.SysInfo,
    @PayloadMcuid() mcuid: string,
  ) {
    return this.productService.buy(dto, user, mcuid);
  }
}
